package struct.intermediate.stack;

import struct.primary.array.Array;

/**
 * @author MAX
 * @version 1.0
 * @description 中级数据结构-数组栈
 * @date 2022/1/13 10:34
 */
public class ArrayStack<E> implements Stack<E>{

    private Array<E> data;

    public ArrayStack() {
        data = new Array<>();
    }

    public ArrayStack(int capacity) {
        data = new Array<>(capacity);
    }
    @Override
    public int size() {
        return data.size();
    }

    @Override
    public boolean isEmpty() {
        return data.isEmpty();
    }

    @Override
    public void push(E e) {
        data.addLast(e);
    }

    @Override
    public E pop() {
        return data.removeLast();
    }

    @Override
    public E peek() {
        return data.get(data.size() - 1);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append("top [");
        for (int i = data.size() - 1; i >= 0; i--) {
            res.append(data.get(i));
            if(i != 0) res.append(",");
        }

        res.append("] bottom");

        return res.toString();
    }
}
